<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/common/taglibs.jsp"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html>
<html lang="en">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<head>
<%@ include file="/common/common-css.jsp"%>
<style>
#jquery-table td {
	cursor: pointer !important
}

.load_bg {
	background: #000;
	opacity: 0.1;
	left: 0px;
	top: 0px;
	position: fixed;
	height: 100%;
	width: 100%;
	overflow: hidden;
	z-index: 10000;
}

.load_img {
	left: 50%;
	top: 180px;
	position: fixed;
	height: 50px;
	overflow: hidden;
	z-index: 20000;
}
</style>
</head>
<body class="no-skin">
	<div class="load_bg" style="display: none;"></div>
	<img src="http://img.xmiles.cn/commentloading.gif" class="load_img"
		style="display: none" />
	<div class="page-content">
		<div class="page-header"
			style="margin-bottom: 0px; padding-bottom: 10px;"></div>

		<form id="queryForm">
			<div class="page-content-area">
				<div class="row">
					<div class="col-sm-12">
						<div class="widget-box"
							style="border: 0px solid #ccc; border-top: none; border-bottom: none;">
							<div class="widget-header widget-header-blue widget-header-flat">
								<h4 class="widget-title">选择条件进行查询</h4>
								<div class="widget-toolbar">
									<a href="#" data-action="collapse"> <i
										class="ace-icon fa fa-chevron-up"></i>
									</a>
								</div>
							</div>
							<div class=" widget-body">
								<div class="widget-main">
									<div class="form-group col-xs-12 col-sm-4 col-md-3 col-lg-3">
										<label class="col-sm-4 control-label"
											style="text-align: right" for="isprize">平台:</label>
										<div class="col-sm-8" style="padding-left: 0;">
											<select class="form-control" name="platform">
												<option value="">全部</option>
												<option value="0">通用</option>
												<option value="android">安卓</option>
												<option value="ios">ios</option>
											</select>
										</div>
									</div>

									<div class="form-group col-xs-12 col-sm-4 col-md-3 col-lg-3">
										<label class="col-sm-4 control-label"
											style="text-align: right" for="isprize">产品:</label>
										<div class="col-sm-8" style="padding-left: 0;">
											<select class="form-control" name="prdid">
												<option value="">全部</option>
												<option value="0">通用</option>
												<option value="-1">车主无忧</option>
												<option value="8000">贷款之家</option>
											</select>
										</div>
									</div>

									<div class="form-group col-xs-12 col-sm-4 col-md-3 col-lg-3">
										<label class="col-sm-4 control-label"
											style="text-align: right" for="isprize">标签:</label>
										<div class="col-sm-8" style="padding-left: 0;">
											<input type="text" style="padding-left: 0px" name="tag"
												id="tag">
										</div>
									</div>

									<div class="form-group col-xs-12 col-sm-4 col-md-3 col-lg-3">
										<label class="col-sm-4 control-label"
											style="text-align: right" for="isprize">状态:</label>
										<div class="col-sm-8" style="padding-left: 0;">
											<select class="form-control" name="status">
												<option value="">全部</option>
												<option value="1">上线</option>
												<option value="0">下线</option>
											</select>
										</div>
									</div>

									<div class="form-group col-xs-12 col-sm-4 col-md-3 col-lg-3">
										<label class="col-sm-4 control-label"
											style="text-align: right" for="isprize">广告位:</label>
										<div class="col-sm-8" style="padding-left: 0;">
											<select class="form-control" name="advert_space"
												id="advert_space">
												<option value="">全部</option>
												<option value="0">通用</option>
												<option value="1">APP首页列表</option>
												<option value="2">贷款TAB列表</option>
												<option value="3">好卡推荐列表</option>
											</select>
										</div>
									</div>
								</div>
							</div>
						</div>
					</div>
					<div class="col-md-12 center">
						<div class="clearfix form-actions">

							<button class="btn btn-purple  no-border" type="button"
								name="btn-query" onclick="query_any()" id="btn-query">
								<i class="ace-icon fa fa-search icon-on-right bigger-110"></i>
								查询
							</button>
							&nbsp;&nbsp;&nbsp;
							<button class="btn btn-grey  no-border" type="reset"
								onclick="Javascript:doAddPro();">
								<i class="ace-icon fa fa-undo icon-on-right bigger-110"></i> 新增
							</button>
							
							&nbsp;&nbsp;&nbsp;
					        <button class="btn btn-purple  no-border" type="button" name="btn-query" onclick="delRedis()" id="btn-query">
						      <i class="ace-icon fa fa-search icon-on-right bigger-110"></i>
						                 删除缓存
					        </button>
					        
							&nbsp;&nbsp;&nbsp;
							<button class="btn btn-purple  no-border" type="button"
								name="btn-query" onclick="updateindex()" id="btn-query">
								<i class="ace-icon fa fa-search icon-on-right bigger-110"></i>
								保存
							</button>
						</div>
					</div>
				</div>
				<div class="row">
					<div class="col-xs-12">
						<table id="jquery-table"
							class="table table-striped table-bordered table-hover">
						</table>
					</div>
				</div>
			</div>
		</form>
	</div>

	<%@ include file="/common/common-js.jsp"%>
	<script type="text/javascript" src="${ctx}/js/call_func.js"></script>
	<script type="text/javascript"
		src="${ctx}/ace/js/jquery.dataTables.min.js"></script>
	<script type="text/javascript">
		var souceList = null;
		//【查询】按钮Click事件
		function query_any() {
			$(".load_bg").removeAttr("style");
			$(".load_img").removeAttr("style");
			if (jqTable != null) {
				jqTable.fnDraw();
			} else {
				loaddata();
			}
		}
		var jqTable = null;
		var loaddata = function() {
			jQuery(function($) {
				jqTable = $('#jquery-table')
						.dataTable(
								{
									//数据列控制（包括对应返回JSON属性名称，对应表头列索引位置、数据转换封装等）
									"columnDefs" : [

											{
												"title" : '排序',
												"targets" : [ 0 ],
												"data" : "", //JSON返回属性
												"bSortable" : false,
												"width" : "5%",
												"render" : function(value,
														type, row) { // 返回自定义内容
													return '<i class="glyphicon glyphiconfir glyphicon-chevron-up" data-tabindex="'+row.id+'"></i><br><i class="glyphicon glyphicon-chevron-down"></i>';
												}
											},
											{
												"title" : '渠道',
												"name" : "channel",
												"data" : "channel", //JSON返回属性
												"targets" : [ 1 ],
												"bSortable" : false,
												"width" : "5%",

											},

											{
												"title" : "活动ID",
												"name" : "id",
												"data" : "id",
												"targets" : [ 2 ],
												"bSortable" : false,
												"width" : "7%",
											},
											{
												"title" : "Logo",
												"name" : "logo",
												"data" : "logo",
												"targets" : [ 3 ],
												"bSortable" : false,
												"width" : "7%",
												"render": function(value, type, row) { // 返回自定义内容
						   		    	  			
							  					      var star="<img src='"+value+"' style='height:60px;'/>";
							  						  return star;
							  					  },
											},
											{
												"title" : "活动名称",
												"name" : "name",
												"data" : "name",
												"targets" : [ 4 ],
												"bSortable" : false,
												"width" : "7%",
											},

											{
												"title" : "顺序",
												"name" : "position",
												"data" : "position",
												"targets" : [ 5 ],
												"bSortable" : false,
												"width" : "5%",
												"render": function(value, type, row) { // 返回自定义内容
													  return '<input class="form-control backup-name" id="valueindex" row="3" data-id='+row.id+' onblur="upperCase(this)" data-index='+value+' value='+value+' style="height:100% !important;"/>';
												  },
											},

											{
												"title" : "操作人",
												"name" : "operator",
												"data" : "operator",
												"targets" : [ 6 ],
												"bSortable" : false,
												"width" : "5%",
											},
											{
												"title" : "上线时间",
												"name" : "starttime",
												"data" : "starttime",
												"targets" : [ 7 ],
												"bSortable" : false,
												"width" : "5%",
											},
											{
												"title" : "操作",
												"name" : "id",
												"data" : "id",
												"targets" : [ 8 ],
												"bSortable" : false,
												"width" : "8%",
												"render" : function(value,
														type, row) { // 返回自定义内容
													var text = "<button type='button' value='"+value+"' name='infoID' class='btn btn-primary btn-sm no-border infoID'>编辑</button>";
													if (row.status == 0) {
														text += "  <a class='btn btn-success btn-sm' href='javascript:void(0)' onclick='updateStatus("
																+ row.id
																+ ",1)'>上线</a>";
													}
													if (row.status == 1) {
														text += "  <a class='btn btn btn-sm' href='javascript:void(0)' onclick='updateStatus("
																+ row.id
																+ ",0)'>下线</a>";
													}
													return text;
												}
											}, ],
									"sAjaxSource" : ctx
											+ "/creditcare/list.action",
									"fnServerData" : function(sSource, aoData,
											fnCallback) {//从服务器请求数据
										var data = $('#queryForm')
												.serializeObject();
										data.aoData = JSON.stringify(aoData);
										$.ajax({
											"dataType" : 'json',
											"type" : "POST",
											"url" : sSource,
											"data" : data,
											"success" : function(resp) {
												$(".load_bg").attr("style",
														"display:none");
												$(".load_img").attr("style",
														"display:none");
												$("#hidden_pass").attr("style",
														"display:block");
												$('#jquery-table').removeClass(
														"dataTable");
												fnCallback(resp);
												initListPage();
											},
											"error" : function(XMLHttpRequest,
													textStatus, errorThrown) {
												$(".load_bg").attr("style",
														"display:none");
												$(".load_img").attr("style",
														"display:none");
												alert('请求服务器错误,请刷新重试！！');//错误提示，包括服务器重启导致的session失效

											}
										});
									},
									"fnRowCallback" : function(nRow, aData,
											iDisplayIndex, iDisplayIndexFull) {
										//=====================这里实现行双击事件======================//
										$(nRow)
												.on(
														'click',
														'td .infoID',
														function(index) {
															var id = $(this)
																	.val();
															var url = "/creditcare/edit.action?id="
																	+ id;
															url = encodeURI(url);
															openDialogWithRand(
																	url,
																	1400,
																	1000,
																	"",
																	function(
																			data) {
																	});
														});
									},
									"bDestroy" : true,
									"bScrollInfinite" : true,
									"sScrollXInner" : "100%",
									"sScrollY" : "100%",//如果不设置，IE会有问题
									"aaSorting" : [ [ 0, "desc" ] ]
								//初始化加载，默认按第几列排序、排序方式（列从0开始计算）
								});
			});
		}

		jQuery(function($) {
			//日期时间插件
			$('.date-picker').datetimepicker({
				language : 'zh-CN',
				weekStart : 1,
				todayBtn : 1,
				autoclose : 1,
				todayHighlight : 1,
				startView : 2,
				forceParse : 0,
				showMeridian : 1,
				format : 'yyyy-mm-dd hh:ii:ss'
			}).next().on(ace.click_event, function() {
				$(this).prev().focus();
			});
		});
		
		function upperCase(obj){
			var id = $(obj).attr("data-id");//替换的id
			var oldIndex = $(obj).attr("data-index");//老顺序
			var newIndex = $(obj).val();//新顺序
			if(newIndex==""){
				alert("顺序不能为空");
				return;
			}
			if(newIndex<0){
				alert("顺序不能少于0");
				return;
			}
			 $.ajax({
				url: ctx+'/creditcare/newSortPosition.action',
				type: 'POST',
				dataType: 'json',
				data: {'oldIndex': oldIndex,'id':id,"newIndex":newIndex},
				success:function(data){
	                console.log(data);
	                query_any();
				},
				error:function(){
					console.log('排序失败');
				}
			}); 
		}

		$(document).ready(function() {
			query_any();			
			//上移
	        $('#jquery-table').delegate('.glyphicon-chevron-up','click',function(){
				var arr = new Array();
				var thispos=$(this).parent('td').parent('tr');
				var thishtml=thispos.clone();
				var prevpos=$(this).parent('td').parent('tr').prev();
				var thisindex = thispos.index();
				if(thisindex==0){
					$('.discovery-up').show();
					setTimeout("$('.discovery-up').hide()",3000);
				}else{
					thispos.remove();
					prevpos.before(thishtml);
					$('#jquery-table tbody tr').each(function(){
						arr.push($(this).find('td').find('.glyphicon-chevron-up').attr('data-tabindex'));
					});
					var arrstring = arr.join("#");
					console.log(arrstring);
					 $.ajax({
						url: ctx+'/creditcare/sortTab.action',
						type: 'POST',
						dataType: 'json',
						data: {"prdSortList": arrstring},
						success:function(data){
	                        console.log(data);
						},
						error:function(){
							console.log('排序失败');
						}
					});
				  }
				});
		        //下移
				$('#jquery-table').delegate('.glyphicon-chevron-down','click',function(){
					var arr = new Array();
					var thispos=$(this).parent('td').parent('tr');
					var thishtml=thispos.clone();
					var nextpos=$(this).parent('td').parent('tr').next();
					var thisindex = thispos.index();
					if(thisindex==$("#jquery-table tbody tr").length - 1){
						$('.discovery-down').show();
						setTimeout("$('.discovery-down').hide()",3000);
					}else{
						thispos.remove();
				        nextpos.after(thishtml);
						$('#jquery-table tbody tr').each(function(){
							arr.push($(this).find('td').find('.glyphicon-chevron-up').attr('data-tabindex'));
						});
						var arrstring = arr.join("#");
						 $.ajax({
							url: ctx+'/creditcare/sortTab.action',
							type: 'POST',
							dataType: 'json',
							data: {'prdSortList': arrstring},
							success:function(data){
		                        console.log(data);
							},
							error:function(){
								console.log('排序失败');
							}
						}); 
						
					}
				}); 
		});

		var flag = true;
		function delRedis() {
			if (flag) {
				var url = ctx + "/creditcare/delRedis.action";
				$.ajax({
					"url" : url,
					"type" : "POST",
					"dataType" : 'json',
					"beforeSend" : function() {
						flag = false;
					},
					"success" : function(data) {
						alert("操作成功");
						query_any();
						flag = true;
					},
					"error" : function() {
						alert("操作失败");
						flag = true;
					},
					"complete" : function() {
						//解除事件
						flag = true;
					}
				});
			}
		}
		
		function doAddPro() {	
			/* var url = "//module/detail.action?id=-1";
			url = encodeURI(url); */
			openDialogWithRand("/creditcare/add.action",1400,1000,"新增信用卡",function (data){});
		}

		function showInvest(status) {
			var ids = "";
			$(":checkbox").each(function() {
				if ($(this).is(':checked')) {
					var id = $(this).val();
					ids += id + ",";
				}
			});
			if (ids == "") {
				return;
			}
			var url = ctx + "/activity/showInvest.action?ids=" + ids
					+ "&status=" + status;
			$.ajax({
				"url" : url,
				"type" : "POST",
				"dataType" : 'json',
				"beforeSend" : function() {
					flag = false;
				},
				"success" : function(data) {
					alert("操作成功");
					query_any();
					flag = true;
				},
				"error" : function() {
					alert("操作失败");
					flag = true;
				},
				"complete" : function() {
					//解除事件
					flag = true;
				}
			});
		}

		function updateindex() {
			var valueindex = new Array();
			var ids = new Array();
			var flag = false;
			$("input[id='valueindex']").each(function(i, obj) {
				var value = $(obj).val();
				var id = $(obj).attr("data-id");
				if (value == "") {
					alert("顺序不能为空！");
					flag = true;
				}
				ids.push(id);
				valueindex.push(value);
			});
			if (flag) {
				return;
			}
			$.ajax({
				url : ctx + '/activity/sortPosition.action',
				type : 'POST',
				dataType : 'json',
				data : {
					'valueindex' : valueindex.toString(),
					'ids' : ids.toString()
				},
				success : function(data) {
					console.log(data);
					query_any();
				},
				error : function() {
					console.log('排序失败');
				}
			});
		}

		function updateStatus(id, status) {
			var url = ctx + "/creditcare/updateStatus.action";
			$.ajax({
				"url" : url,
				"type" : "POST",
				"dataType" : 'json',
				"data" : {
					"id" : id,
					"status" : status
				},
				"beforeSend" : function() {
				},
				"success" : function(data) {
					alert("操作成功");
					query_any();
				},
				"error" : function() {
					alert("操作失败");
				},
				"complete" : function() {
					//解除事件
				}
			});
		}


		var check = true;
		function checkAll() {
			if (check) {
				$(":checkbox").prop("checked", true);
				check = false;
			} else {
				$(":checkbox").prop("checked", false);
				check = true;
			}
		}
	</script>

</body>
</html>